use std::mem::MaybeUninit;
use sqlx::{SqlitePool, Executor};
use crate::error::AppErr;

static mut POOL: MaybeUninit<SqlitePool> = MaybeUninit::uninit();

#[cfg(debug_assertions)]
const PATH: &'static str = "sqlite://../data.db?mode=rwc";

#[cfg(not(debug_assertions))]
const PATH: &'static str = "sqlite://./data.db?mode=rwc";

mod pos;
mod linkage;

pub use pos::*;
pub use linkage::*;

pub async fn init() -> Result<(), AppErr> {

    let pool = SqlitePool::connect(PATH).await?;
    pool.execute(SQL_NAMED_POS_CREATE).await?;
    pool.execute(SQL_LINKAGE_CREATE).await?;
    pool.execute(SQL_LINKAGE_POS_CREATE).await?;
    unsafe {
        POOL.write(pool);
    }
    Ok(())
}

#[inline]
pub fn get_pool() -> &'static SqlitePool {
    unsafe {
        POOL.assume_init_ref()
    }
}

